package cn.edu.lingnan.task;

import cn.edu.lingnan.service.ExamService;
import cn.edu.lingnan.service.StatisticsService;
import cn.edu.lingnan.service.TaskService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ExamScheduler {

      @Autowired
      private TaskService taskService;

      @Autowired
      private ExamService examService;

      @Autowired
      private StatisticsService statisticsService;
    /**
     * 每分钟执行一次，检查并更新考试状态
     * Cron 表达式解释:
     * "0 0/1 * * * ?" - 每分钟的第0秒执行
     */
    @XxlJob("update-exam-status")
    public void updateExamStatuses() {
        System.out.println("执行考试状态更新任务: " + java.time.LocalDateTime.now());
        taskService.publishExams();
        taskService.endExams();
        System.out.println("执行未交卷考试状态更新任务: " + java.time.LocalDateTime.now());
        examService.markUnsubmittedExams();

    }

    //每晚2点执行一次，统计所有已结束的考试的成绩
    @XxlJob("examScoreStatistics")
    public void gradeAllEndedExams() {
        statisticsService.gradeAllEndedExams();
    }
}
